$(function () {
    var staff_datagrid = $("#staff_datagrid");
    var familyBackground_datagrid = $("#familyBackground_datagrid");
    var personageSalary_datagrid = $("#personageSalary_datagrid");
    var workExperience_datagrid = $("#workExperience_datagrid");
    var situationOfEducation_datagrid = $("#situationOfEducation_datagrid");
    var trainingSituation_datagrid = $("#trainingSituation_datagrid");
    var otherCases_datagrid = $("#otherCases_datagrid");
    var staff_dialog = $("#staff_dialog");
    var particular_dialog = $("#particular_dialog");
    var staff_form = $("#staff_form");
    var particular_form = $("#particular_form");
    var personageSalary_form = $("#personageSalary_form");
    //人力资源列表
    staff_datagrid.datagrid({
        url: '/staff/list',
        striped: "true",
        pagination: "true",
        singleSelect: "true",
        fit: "true",
        title: "条件查询",
        fitColumns: "true",
        pageSize: 10,
        pageList: [3, 5, 10, 20, 30],
        toolbar: '#staff_toolbar',
        columns: [[
            {field: 'id', title: '编号', width: 100},
            {field: 'name', title: '姓名', width: 100},
            {field: 'jobNumber', title: '工号', width: 100},
            {
                field: 'department', title: '部门', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.name : "未知部门";
                }
            },
            {
                field: 'duty', title: '职务', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {field: 'email', title: '邮箱', width: 100},
            {
                field: 'way', title: '聘用方式', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },

            {
                field: 'education', title: '学历', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {
                field: 'post', title: '岗位', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {
                field: 'gender', title: '性别', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {field: 'age', title: '年龄', width: 100},
            {
                field: 'place', title: '籍贯', width: 100,
                formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {field: 'phone', title: '电话', width: 100},
            {field: 'checkNumber', title: '考勤卡', width: 100}
        ]]
    })


    //保存/编辑对话框 初始化
    staff_dialog.dialog({
        width: 900,
        height: 550,
        fitColumns: "true",
        closed: false,
        closed: true,
        buttons: "#dialog_buttons"
    });
    //保存/编辑对话框 初始化 ----end

    //代码优化
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });
    //-------------------------------------
    var staffId;
    var cmdObj = {
        reload: function () {
            staff_datagrid.datagrid("reload");
        },
        add: function () {
            //清除form 表单 缓存数据
            staff_form.form('clear');
            staff_dialog.dialog("setTitle", "工人保存");
            staff_dialog.dialog("open");
        },
        //界面刷新
        //-------------------------------------
        reload: function () {
            $("#staff_datagrid").datagrid("load");
        },
        //-------------------------------------
        //关闭窗口
        closes: function () {
            staff_dialog.dialog("close");
        },
        //-------------------------------------
        //工人保存按钮
        save: function () {
            //提交表单
            staff_form.form("submit", {
                //提交表单数据的路径
                url: "/staff/saveOrUpdate",
                //提交成功后的回调函数,返回参数是json格式的字符串
                success: function (date) {
                    //转换成json对象
                    var date = $.parseJSON(date)
                    //success属性为true表示成功
                    if (date.success) {
                        $.messager.alert('温馨提示', '操作成功', "ok", function () {
                            //关闭窗口
                            cmdObj.closes();
                            //刷新页面
                            cmdObj.reload();
                        });
                        //success属性为false表示失败
                    } else {
                        $.messager.alert('温馨提示', date.msg, "error", function () {
                        });
                    }
                }
            });
        },
        //-------------------------------------
        //工人编辑
        edit: function () {
            //返回选中的行的对象,没选返回null
            var row = staff_datagrid.datagrid("getSelected");
            //判断是否选中行,没有选返回提示信息,结束方法
            if (!row) {
                $.messager.alert('温馨提示', '请选择要编辑的行');
            } else {
                //选中行后的操作
                //修改对话框标题头文本
                staff_dialog.dialog("setTitle", "工人编辑");
                //打开编辑窗口,默认是关闭
                staff_dialog.dialog("open");
                //清除表单数据
                staff_form.form("clear");
                //为表单添加dept.id属性,回显需要匹配同名原则
                if (row.department) {
                    row["department.id"] = row.department.id;
                }
                ;
                if (row.dept) {
                    row["dept.id"] = row.dept.id;
                }
                ;
                if (row.duty) {
                    row["duty.id"] = row.duty.id;
                }
                ;
                if (row.way) {
                    row["way.id"] = row.way.id;
                }
                ;
                if (row.education) {
                    row["education.id"] = row.education.id;
                }
                ;
                if (row.post) {
                    row["post.id"] = row.post.id;
                }
                ;
                if (row.gender) {
                    row["gender.id"] = row.gender.id;
                }
                ;
                if (row.place) {
                    row["place.id"] = row.place.id;
                }
                ;
                if (row.postrank) {
                    row["postrank.id"] = row.postrank.id;
                }
                ;
                if (row.nation) {
                    row["nation.id"] = row.nation.id;
                }
                ;
                if (row.identity) {
                    row["identity.id"] = row.identity.id;
                }
                ;
                if (row.politics) {
                    row["politics.id"] = row.politics.id;
                }
                ;
                if (row.permanent) {
                    row["permanent.id"] = row.permanent.id;
                }
                ;
                if (row.marital) {
                    row["marital.id"] = row.marital.id;
                }
                ;
                //读取行里的数据到表单中,用于回显
                staff_form.form("load", row);
            }
        },
        //-------------------------------------
        //判断是否选中
        changeState: function () {
            //判断是否选中
            var row = staff_datagrid.datagrid("getSelected")
            if (row == null) {
                $.messager.alert('温馨提示', "请选中需要编辑的员工");
                return;
            }
            $.get("/staff/changeState?id=" + row.id, function (data) {
                if (data.succee) {
                    staff_datagrid.datagrid("reload")
                } else {
                    $.messager.alert('温馨提示', data.msg);
                }
            })
        },
        //-------------------------------------
        //详细详细按钮
        particular: function () {

            var row = staff_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "需要查看哪位工人信息");
            } else {
                staffId = row.id;
               /* var  url =  "/idCardPicture/imagePath?staffId=" + staffId
                 $.get(url,function(data) {
                 console.log(data);
                 });*/
                //选中行后的操作
                //清除表单数据
                personageSalary_form.form("clear");
                //为表单添加属性,回显需要匹配同名原则
                if (row.department) {
                    row["department.id"] = row.department.id;
                }
                ;
                if (row.duty) {
                    row["duty.id"] = row.duty.id;
                }
                ;
                if (row.postrank) {
                    row["postrank.id"] = row.postrank.id;
                }
                ;
                if (row.post) {
                    row["post.id"] = row.post.id;
                }
                ;
                if (row.way) {
                    row["way.id"] = row.way.id;
                }
                ;
                if (row.permanent) {
                    row["permanent.id"] = row.permanent.id;
                }
                ;
                //--------------------------------------
                if (row.personageSalary){
                    if (row.personageSalary.scaleType) {
                        row["scaleType.id"] = row.personageSalary.scaleType.id;
                    }
                    ;
                    if (row.personageSalary.accountType) {
                        row["accountType.id"] = row.personageSalary.accountType.id;
                    }
                    ;
                    if (row.personageSalary.bonusTypes) {
                        row["bonusTypes.id"] = row.personageSalary.bonusTypes.id;
                    }
                    ;
                    if (row.personageSalary.otType) {
                        row["otType.id"] = row.personageSalary.otType.id;
                    }
                };
                //读取行里的数据到表单中,用于回显
                personageSalary_form.form("load", row);
                personageSalary_form.form("load", row.personageSalary);
                $("#particular_tabs").window({
                    width: 950,
                    height: 600,
                    resizable: false,
                    maximizable: false,
                    inline: true,
                    title: "详细信息",
                    buttons: "#dialog_buttons",
                    onSelect: function (title, index) {
                        alert(title, index);
                    }
                });

                //家庭情况选项卡
                familyBackground_datagrid.edatagrid({
                    url: '/familyBackground/byStaffList?staffId=' + staffId,
                    saveUrl: 'familyBackground/saveOrUpdate?staffId=' + staffId,
                    updateUrl: 'familyBackground/saveOrUpdate?staffId=' + staffId,
                    destroyUrl: 'familyBackground/delete',
                    striped: "true",
                    pagination: "true",
                    singleSelect: 'true',
                    autoSave: 'true',
                    width: 920,
                    height: 520,
                    pageSize: 10,
                    pageList: [3, 5, 10, 20, 30],
                    toolbar: '#familyBackground_toolbar',
                    columns: [[
                        {field: 'title', title: '称谓', width: 153, editor: 'text'},
                        {field: 'name', title: '姓名', width: 153, editor: 'text'},
                        {field: 'workUnit', title: '工作单位', width: 153, editor: 'text'},
                        {field: 'organization', title: '职业', width: 153, editor: 'text'},
                        {field: 'dateOfBirth', title: '出生日期', width: 153, editor: 'datebox'},
                        {field: 'remark', title: '备注', width: 153, editor: 'text'}
                    ]]
                })
                //工作经历选项卡
                workExperience_datagrid.edatagrid({
                    url: '/workExperience/byStaffList?staffId=' + staffId,
                    saveUrl: 'workExperience/saveOrUpdate?staffId=' + staffId,
                    updateUrl: 'workExperience/saveOrUpdate',
                    destroyUrl: 'workExperience/delete',
                    striped: "true",
                    pagination: "true",
                    singleSelect: 'true',
                    autoSave: 'true',
                    width: 920,
                    height: 520,
                    pageSize: 10,
                    pageList: [3, 5, 10, 20, 30],
                    toolbar: '#workExperience_toolbar',
                    columns: [[
                        {field: 'organization', title: '工作单位', width: 153, editor: 'textbox'},
                        {field: 'occupation', title: '职业', width: 153, editor: 'textbox',},
                        {field: 'duty', title: '职务', width: 153, editor: 'textbox',},
                        {field: 'beginDate', title: '开始日期', width: 153, editor: 'datebox'},
                        {field: 'endDate', title: '结束日期', width: 153, editor: 'datebox'},
                        {field: 'remark', title: '备注', width: 153, editor: 'textbox'}
                    ]]
                })
                //教育情况选项卡
                situationOfEducation_datagrid.edatagrid({
                    url: '/situationOfEducation/byStaffList?staffId=' + staffId,
                    saveUrl: 'situationOfEducation/saveOrUpdate?staffId=' + staffId,
                    updateUrl: 'situationOfEducation/saveOrUpdate',
                    destroyUrl: 'situationOfEducation/delete',
                    striped: "true",
                    pagination: "true",
                    singleSelect: 'true',
                    autoSave: 'true',
                    width: 920,
                    height: 520,
                    pageSize: 10,
                    pageList: [3, 5, 10, 20, 30],
                    toolbar: '#situationOfEducation_toolbar',
                    columns: [[
                        {field: 'schoolOfGraduation', title: '毕业学校', width: 153, editor: "text"},
                        {field: 'major', title: '所学专业', width: 153, editor: "text"},
                        {field: 'certifier', title: '证明人', width: 153, editor: "text"},
                        {field: 'beginDate', title: '开始日期', width: 153, editor: "datebox"},
                        {field: 'endDate', title: '结束日期', width: 153, editor: "datebox"},
                        {field: 'remark', title: '备注', width: 153, editor: "text"}
                    ]]
                })
                //培训情况选项卡
                trainingSituation_datagrid.edatagrid({
                    url: '/trainingSituation/byStaffList?staffId=' + staffId,
                    saveUrl: 'trainingSituation/saveOrUpdate?staffId=' + staffId,
                    updateUrl: 'trainingSituation/saveOrUpdate',
                    destroyUrl: 'trainingSituation/delete',
                    striped: "true",
                    pagination: "true",
                    autoSave: 'true',
                    width: 920,
                    height: 520,
                    pageSize: 10,
                    pageList: [3, 5, 10, 20, 30],
                    toolbar: '#trainingSituation_toolbar',
                    columns: [[
                        {field: 'trainingTitlee', title: '培训名称', width: 101, editor: "text"},
                        {field: 'reasonForTraining', title: '培训类型', width: 101, editor: "text"},
                        {field: 'trainingInstitution', title: '培训机构', width: 101, editor: "text"},
                        {field: 'trainingCost', title: '培训费用', width: 101, editor: "text"},
                        {field: 'houseTraining', title: '培训场所', width: 101, editor: "text"},
                        {field: 'trainingresultss', title: '学历要求', width: 101, editor: "text"},
                        {field: 'beginDate', title: '开始日期', width: 101, editor: "datebox"},
                        {field: 'endDate', title: '结束日期', width: 101, editor: "datebox"},
                        {field: 'remark', title: '备注', width: 101, editor: "text"}
                    ]]
                })
                //其它情况选项卡
                otherCases_datagrid.edatagrid({
                    url: '/otherCases/byStaffList?staffId=' + staffId,
                    updateUrl: 'otherCases/saveOrUpdate',
                    destroyUrl: 'otherCases/delete',
                    striped: "true",
                    pagination: "true",
                    singleSelect: 'true',
                    autoSave: 'true',
                    width: 920,
                    height: 520,
                    pageSize: 10,
                    pageList: [3, 5, 10, 20, 30],
                    toolbar: '#otherCases_toolbar',
                    columns: [[
                        {field: 'dateOfContract', title: '合同签订日期', width: 153, editor: "datebox"},
                        {field: 'contractNumber', title: '合同号', width: 153, editor: "textbox"},
                        {field: 'termOfContract', title: '合同期限', width: 153, editor: "text"},
                        {field: 'contractExpiryDate', title: '合同到期日期', width: 153, editor: "datebox"},
                        {field: 'stayNumber', title: '暂住证号', width: 153, editor: "text"},
                        {field: 'remark', title: '备注', width: 153, editor: "text"}
                    ]]
                })
            }
        },
        //-------------------------------------
        //工资保存
        personageSalary_save: function () {
            //提交表单
            var row = staff_datagrid.datagrid("getSelected");
            if (row.personageSalary) {
                row["id"] = row.personageSalary.id;
            };
            personageSalary_form.form("submit", {
                //提交表单数据的路径
                url: "/staff/PersonageSalary_saveOrUpdate?staffId=" + row.id,
                //提交成功后的回调函数,返回参数是json格式的字符串
                success: function (date) {
                    //转换成json对象
                    var date = $.parseJSON(date)
                    //success属性为true表示成功
                    if (date.success) {
                        $.messager.alert('温馨提示', '操作成功', "ok", function () {
                            //关闭窗口
                            cmdObj.closes();
                            //刷新页面
                            cmdObj.reload();
                        });
                        //success属性为false表示失败
                    } else {
                        $.messager.alert('温馨提示', date.msg, "error", function () {
                            //关闭窗口
                            cmdObj.closes();
                            //刷新页面
                            cmdObj.reload();
                        });
                    }
                }
            });
        },
        //工资关闭
        personageSalary_closes: function () {
            $("#particular_tabs").dialog("close");
        },

        familyBackground_add: function () {
            familyBackground_datagrid.datagrid("appendRow", {});
        },
        //staff高级查询
        reload: function () {
            var name = $("#name").textbox("getValue");
            var way = $("#way").combobox("getValue");
            var duty = $("#duty").combobox("getValue");
            var education = $("#education").combobox("getValue");

            staff_datagrid.datagrid("load", {
                name: name,
                way: way,
                duty: duty,
                education: education
            });
        },

        save_picture: function () {
            $("#editForm").form("submit", {
                //提交表单数据的路径
                url: "/idCardPicture/saveOrUpdate?staffId=" + staffId,
                //提交成功后的回调函数,返回参数是json格式的字符串
                success: function (date) {
                    //转换成json对象
                    var date = $.parseJSON(date)
                    //success属性为true表示成功
                    if (date.success) {
                        $.messager.alert('温馨提示', '上传成功', "ok", function () {
                            //关闭窗口
                            cmdObj.closes();
                            //刷新页面
                            cmdObj.reload();
                        });
                        //success属性为false表示失败
                    } else {
                        $.messager.alert('温馨提示', date.msg, "error", function () {
                            //关闭窗口
                            cmdObj.closes();
                            //刷新页面
                            cmdObj.reload();
                        });
                    }
                }
            });
        }
    }


    $("#staff_filebox").filebox({
        buttonText: '选择图片',
        accept: "jpg"
    })
})

//添加一行空行
function staff_addRow(id) {
    $("#" + id).edatagrid("addRow");
}

//删除一行数据
function staff_destroyRow(datagrid) {
    var datagrid = $("#" + datagrid);
    var row = datagrid.edatagrid("getSelected");
    var index = datagrid.edatagrid("getRowIndex", row);
    if (!row) {
        $.messager.alert('温馨提示', "需要删除哪条数据");
    } else {
        datagrid.edatagrid("destroyRow", index);
        // datagrid.edatagrid("load");
    }
}